package com.credithc.customers.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.customers.api.service.ICustomerInfoService;
import com.credithc.customers.api.utils.RequestUtils;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.base.ro.*;
import com.credithc.framework.log.annotation.Trace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @Title: 客户信息维护
 * @Author: Denghx
 * @Date: 2018/12/2 21:19
 * @Description: com.credithc.customers.api.controller
 * @Version: V1.0.0
 */
@RestController
@RequestMapping("/api/yzt/v1")
@Slf4j
@Trace
public class CustomerInfoController {
    @Autowired
    MessageFactory messageFactory;
    @Autowired
    ICustomerInfoService customerInfoService;
    @Autowired
    RequestUtils requestUtils;

    /**
     * @description: 接受客户证件信息变更通知
     * @author: denghx
     * @date: 2018/12/2 21:21
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerInfo/receive")
    public Message bizModify(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<BizModifyRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<BizModifyRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, Object> map = customerInfoService.bizModify(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("接受客户证件信息变更接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * 客户角色关系上送
     *
     * @description:
     * @author: denghx
     * @date: 2018/12/3 11:30
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerInfo/relation")
    public Message customerRelation(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerRelationRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerRelationRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            customerInfoService.customerRelation(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("客户角色关系接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * @description: 三要素核验查询
     * @author: denghx
     * @date: 2018/12/3 12:48
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerInfo/queryThreeElements")
    public Message queryThreeElements(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<ThreeElementsRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<ThreeElementsRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, Object> map = customerInfoService.queryThreeElements(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("客户三要素查询接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * 新增信息
     *
     * @param request request
     * @return message
     */
    @PostMapping("/customerInfo")
    public Message addCustomerInfo(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerInfoAddRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerInfoAddRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, String> map = customerInfoService.addCustomerInfo(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("客户新增信息接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * @description: 客户基本信息查询
     * @author: denghx
     * @date: 2018/12/4 0:14
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerInfo/queryCustomer")
    public Message queryCustomerInfo(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerQueryRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerQueryRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, Object> map = customerInfoService.getCustomerInfo(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("客户基本信息查询接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * @description: 客户账户信息查询
     * @author: denghx
     * @date: 2018/12/9 21:35
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerInfo/queryAccount")
    public Message queryAccount(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerAccountRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerAccountRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, Object> map = customerInfoService.queryAccount(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("批量客户接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * @description: 语音客服机器人自助查询
     * @author: fby
     * @date: 2019/7/8 15:43
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/verify/byMobileAndId")
    public Message byMobileAndId(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerQuerySubRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerQuerySubRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, Object> map = customerInfoService.byMobileAndId(bRequest);
            if (map.get("msg") != null) {
                return messageFactory.getInstance(Integer.parseInt(map.get("code").toString()), map.get("msg").toString(), map.get("resMap"));
            }
//            Map<String, Object> map = customerInfoService.byMobileAndId(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map.get("resMap"));
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("语音客服机器人自助查询接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }
}
